package com.example.factory.api.dto.assembler;

import com.example.factory.api.dto.DtoFactory;
import com.example.factory.api.dto.form.DtoFactoryForm;
import com.example.factory.core.domain.FactoryEntity;
import com.example.factory.core.repository.FactoryRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * @author Lei Tao
 */
@Component
public class FactoryAssembler {
    private final FactoryRepository factoryRepository;

    public FactoryAssembler(FactoryRepository factoryRepository) {
        this.factoryRepository = factoryRepository;
    }

    public FactoryEntity toFactoryEntity(DtoFactoryForm dtoFactoryForm) {
        FactoryEntity factoryEntity = factoryRepository.findByGuid(dtoFactoryForm.getGuid());
        BeanUtils.copyProperties(dtoFactoryForm, factoryEntity);
        return factoryEntity;
    }

    public DtoFactory toDtoFactory(FactoryEntity factoryEntity) {
        DtoFactory dtoFactory = new DtoFactory();
        BeanUtils.copyProperties(factoryEntity, dtoFactory);
        return dtoFactory;
    }
}
